﻿Shader "Custom/ScreenWaterDrop"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _ScreenWaterDropTex ("Screen Water", 2D) = "white" {}
    }
    SubShader
    {
        CGINCLUDE

        #include "UnityCG.cginc"

        sampler2D _MainTex, _ScreenWaterDropTex;
		float2 _MainTex_TexelSize;

        float _CurTime;
		float _DropSpeed;
		float _SizeX;
		float _SizeY;
		float _Distortion;

        struct a2v
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
        };
        
        struct v2f 
        {
            float4 pos : SV_POSITION;
            float2 uv : TEXCOORD0;
        };

        v2f vert(a2v v)
        {
            v2f i;
            i.pos = UnityObjectToClipPos(v.vertex);
            i.uv = v.uv;
            return i;
        }

        ENDCG

        pass
        {
            CGPROGRAM
            
            #pragma vertex vert
            #pragma fragment frag

            fixed4 frag(v2f i) : SV_TARGET
            {
                float2 uv = i.uv;

                float2 uv1 = float2(uv.x * 1.15 * _SizeX, 
                                   (uv.y * _SizeY * 1.1) + _CurTime * _DropSpeed * 0.15);

                float2 uv2 = float2(uv.x * 1.25 * _SizeX, 
                                   (uv.y * _SizeY * 1.2) + _CurTime * _DropSpeed * 0.2);

                float2 uv3 = float2(uv.x * 0.9 * _SizeX, 
                                   (uv.y * _SizeY * 1.3) + _CurTime * _DropSpeed * 0.032);

                float3 rainTex1 = tex2D(_ScreenWaterDropTex, uv1).rgb / _Distortion;
                float3 rainTex2 = tex2D(_ScreenWaterDropTex, uv2).rgb / _Distortion;
				float3 rainTex3 = tex2D(_ScreenWaterDropTex, uv3).rgb / _Distortion;
 
				float2 finalRainTex = uv - (rainTex1.xy - rainTex2.xy - rainTex3.xy) / 3;
				float3 finalColor = tex2D(_MainTex, finalRainTex).rgb;

                return fixed4(finalColor, 1);
            }
            
            ENDCG
        }
        
    }
    FallBack "Diffuse"
}
